package stackoverflow.web;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import stackoverflow.domain.question.Tag;
import stackoverflow.domain.service.QuestionService;
import stackoverflow.domain.shared.IdGenerator;
import stackoverflow.domain.user.User;
import stackoverflow.domain.user.UserRepository;

@Controller("addQuestionController")
@RequestMapping("/addQuestion")
public class AddQuestionController {
	
	private UserRepository userRepository;
	private QuestionService questionService;
	
	public AddQuestionController(){}
	
	public AddQuestionController
		(@Qualifier("questionServiceImpl") QuestionService questionService,
		 @Qualifier("userRepositoryImpl") UserRepository userRepository)
	{
		this.questionService = questionService;
		this.userRepository = userRepository;
	}
	
	@Resource(name="questionServiceImpl")
	public void setQuestionService(QuestionService questionService)
	{
		this.questionService = questionService;
	}	

	@Resource(name="userRepositoryImpl")
	public void setUserRepository(UserRepository userRepository)
	{
		this.userRepository= userRepository; 
	}	

	@RequestMapping(value="/addQuestionForm.htm", method = RequestMethod.GET)
	public String showAddQuestion()
	{
		return "addQuestionForm";
		//return "usersOnly/addQuestionForm";
	}
	
	@RequestMapping(value="/addQuestion.htm", method = RequestMethod.POST)
	public String addQuestion
		(@RequestParam("questionText") String questionText,
		 @RequestParam("userId") String userId)
	{
		User user = userRepository.find(userId);
		if(user==null)
		{}
		List<Tag> tags = new ArrayList<Tag>();
		tags.add(new Tag(new IdGenerator(), "Java"));
		questionService.askQuestion(user, questionText, tags);
		return "questionAdded";
		//return "usersOnly/questionAdded";
	}
}
